using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TrafficTrouble.Core
{
    public class TextButton
    {
        private static Vector2 Margin { get { return new Vector2(5, 0); } }

        private static Texture2D BorderTex, HighlightTex;
        private static SpriteFont TextFont;
        public static void SetContent(GraphicsDevice device, SpriteFont textFont)
        {
            TextFont = textFont;

            BorderTex = new Texture2D(device, 1, 1);
            BorderTex.SetData<Color>(new Color[] { Color.White });

            HighlightTex = new Texture2D(device, 1, 1);
            HighlightTex.SetData<Color>(new Color[] { Color.White * .1f });
        }

        public Vector2 Location { get; set; }

        private string _text;
        public string Text
        {
            get { return _text; }
            set { _text = value; this.computeSize(); }
        }

        private Vector2 size;
        public Vector2 Size { get { return size; } }
        private Vector2 TextSize;

        public bool IsPressed { get; set; }

        public TextButton(string text)
            : this(text, float.NegativeInfinity, float.NegativeInfinity) { }
        public TextButton(string text, Vector2 location)
        {
            this.Location = location;
            this.Text = text;
            this.IsPressed = false;
        }
        public TextButton(string text, float x, float y)
            : this(text, new Vector2(x, y)) { }

        private void computeSize()
        {
            this.TextSize = TextFont.MeasureString(this.Text);

            this.size = this.TextSize + 2 * Margin;
        }

        public bool ContainsLocation(Vector2 location)
        {
            return (location.X >= this.Location.X) && (location.X < this.Location.X + this.Size.X)
                && (location.Y >= this.Location.Y) && (location.Y < this.Location.Y + this.Size.Y);
        }

        public void Draw(SpriteBatch g)
        {
            if (this.IsPressed)
                g.Draw(HighlightTex,
                    new Rectangle((int)this.Location.X, (int)this.Location.Y,
                        (int)this.Size.X, (int)this.Size.Y), Color.White);

            g.Draw(BorderTex,
                new Rectangle((int)this.Location.X, (int)this.Location.Y,
                    1, (int)this.Size.Y), Color.White);
            g.Draw(BorderTex,
                new Rectangle((int)this.Location.X, (int)this.Location.Y,
                    (int)this.Size.X, 1), Color.White);
            g.Draw(BorderTex,
                new Rectangle((int)this.Location.X + (int)this.Size.X - 1,
                    (int)this.Location.Y, 1, (int)this.Size.Y), Color.White);
            g.Draw(BorderTex,
                new Rectangle((int)this.Location.X, (int)this.Location.Y + (int)this.Size.Y - 1,
                    (int)this.Size.X, 1), Color.White);

            g.DrawString(TextFont, this.Text, this.Location + (this.Size - this.TextSize) / 2f, Color.White);
        }
    }
}
